import { Context } from 'koa';
import { getManager } from 'typeorm';

import { User } from '../entity/user';
import { ForbiddenException, NotFoundException } from '../exceptions';

export default class UserController {
  public static async listUsers(ctx: Context) {
    const userRepository = getManager().getRepository(User);
    const users = await userRepository.find();

    ctx.status = 200;
    ctx.body = users;
  }

  public static async showUserDetails(ctx: Context) {
    const userRepository = getManager().getRepository(User);
    const user = await userRepository.findOne(+ctx.params.id);

    if(user){
      ctx.status = 200;
      ctx.body = user;
    }else{
      throw new NotFoundException();
    }
  }

  public static async updateUser(ctx: Context) {
    const userId = +ctx.params.id;
    if(userId !== +ctx.state.user.id){
      ctx.status = 403;
      throw new ForbiddenException();
      return ;
    }

    const userRepository = getManager().getRepository(User);
    await userRepository.update(userId, ctx.request.body);
    const updateUser = await userRepository.findOne(userId);

    if(updateUser){
      ctx.status = 200;
      ctx.body = updateUser;
    }else{
      ctx.status = 404;
    }
  }

  public static async deleteUser(ctx: Context) {
    const userId = +ctx.params.id;
    if(userId !== +ctx.state.user.id){
      ctx.status = 403;
      throw new ForbiddenException();
      return ;
    }
    const userRepository = getManager().getRepository(User);
    await userRepository.delete(userId);
    ctx.status = 204;
  }

  public static hello(ctx:Context) {
    ctx.body = 'Hello World';
    ctx.status = 200;
  }
}